package com.rey.demo.api;

import com.rey.demo.link.ZookeeperDemo;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

/**
 * @ClassName: AuthApi
 * @Description: TODO
 * @Author reyJojo
 * @Date 2021/12/3 12:51
 * @Version 1.0
 */
public class AuthApi {
    public static void main(String[] args) {
        try {
            ZooKeeper zooKeeper = ZookeeperDemo.link4ZookeeperApi();
            zooKeeper.addAuthInfo("digest", "wu".getBytes());
            // 创建带权限验证的节点
//            zooKeeper.create("/auth_node", "auth node".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
            // 其他客户端获取该节点（KeeperErrorCode = NoAuth for /auth_node）
            zooKeeper = ZookeeperDemo.link4ZookeeperApi();
            zooKeeper.addAuthInfo("digest", "wu".getBytes());
            byte[] data = zooKeeper.getData("/auth_node", false, null);
            System.out.println(new String(data));
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
